package com.jinzhi.jzweb.controller.moduleConfig;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.pojo.ModuleConfig.*;
import com.jinzhi.jzweb.service.moduleConfig.JzJinzhuanModuleContentService;
import com.jinzhi.jzweb.service.moduleConfig.JzJinzhuanModuleDetailService;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.math3.geometry.partitioning.BSPTree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张唐清
 * @since 2023-06-16
 */
@Controller
@RequestMapping("/jz-jinzhuan-module-content")
@Api(value = "模板任务细则",tags = "模板任务细则")
public class JzJinzhuanModuleContentController extends BaseController {

    /**
     * 业务层，调用业务层方法实现功能
     * */
    @Autowired
    private JzJinzhuanModuleContentService jzJinzhuanModuleContentService;

    @Autowired
    private JzJinzhuanModuleDetailService jzJinzhuanModuleDetailService;


    @ApiOperation(value = "模块任务细则页面入口", notes = "模块任务细则页面入口")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/moduleContent")
//    @RequiresPermissions("jzweb:disposition:moduleContent")
    public String ModuleContent(){
        return "jzweb/moduleContent/getModuleContentEntrance";
    }


    @ApiOperation(value = "模板任务细则新增页面入口", notes = "模板任务细则新增页面入口")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping(value = "/insertModuleContent")
    @RequiresPermissions("jzweb:disposition:insertModuleContent")
    public String insertModuleContent(){return "jzweb/moduleContent/insertModuleContent";}



    @ApiOperation(value = "新增任务细则", notes = "新增任务细则")
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500,message = "服务器内部异常")})
    @ResponseBody
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Result<String> insert(@RequestBody JzJinzhuanModuleDetail jzJinzhuanModuleDetail){
        Date date = new Date();
        jzJinzhuanModuleDetail.setCreaterDate(date);
        boolean ms = jzJinzhuanModuleDetailService.insert(jzJinzhuanModuleDetail);
        jzJinzhuanModuleDetail.setJzJinzhuanModuleContent(Optional.ofNullable(jzJinzhuanModuleDetail.getJzJinzhuanModuleContent()).orElse(new ArrayList<JzJinzhuanModuleContent>()));
        jzJinzhuanModuleDetail.getJzJinzhuanModuleContent().forEach(JzJinzhuanModuleContent -> {
            Date date1 = new Date();
            JzJinzhuanModuleContent.setModuleContentId(jzJinzhuanModuleDetail.getId());//将模板id存入
            JzJinzhuanModuleContent.setCreaterDate(date1);
            jzJinzhuanModuleContentService.insert(JzJinzhuanModuleContent);//存入模板任务数据（存入裁判id数据）
        });
        return  ms ? Result.ok() : Result.fail();
    }


    @ApiOperation(value = "模板任务细则修改页面入口", notes = "模板任务细则修改页面入口")
    @ApiResponses({@ApiResponse(code = 0,message = "操作成功"),
            @ApiResponse(code = 500,message = "服务器内部异常")})
    @GetMapping(value = "/updateModule/{id}")
    @RequiresPermissions("jzweb:disposition:updateModule")
    public String updateModuleContent(@PathVariable("id") Long id, Model model){
        JzJinzhuanModuleDetail jzJinzhuanModuleDetail = new JzJinzhuanModuleDetail();
        jzJinzhuanModuleDetail.setId(id);
        model.addAttribute("modelContent",jzJinzhuanModuleDetailService.getModuleDetail(jzJinzhuanModuleDetail));
        return "jzweb/moduleContent/updateModuleContent";
    }



    @ApiOperation(value = "修改模块细则信息", notes = "修改模块细则信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JzJinzhuanModuleContent", value = "JzJinzhuanModuleContent", required = true, dataType = "JzJinzhuanModuleContent"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "内部服务器异常")})
    @ResponseBody
    @RequestMapping(value = "/updateModuleContentById",method = RequestMethod.POST)
    public Result<String> updateModuleContentById(@RequestBody JzJinzhuanModuleDetail jzJinzhuanModuleDetail){
        //删除模板下的具体细则
        Optional.ofNullable(jzJinzhuanModuleDetail)
                .map(JzJinzhuanModuleDetail::getId)
                .orElseThrow(() -> new RuntimeException("根据id修改没有id"));
        //根据module_id删除数据库中对应的模板任务
        jzJinzhuanModuleContentService.delete(new EntityWrapper<JzJinzhuanModuleContent>().eq("module_content_id",jzJinzhuanModuleDetail.getId()));
        //新增所属任务
        if(!ObjectUtils.isEmpty(jzJinzhuanModuleDetail.getJzJinzhuanModuleContent())){
            jzJinzhuanModuleContentService.insertBatch(jzJinzhuanModuleDetail.getJzJinzhuanModuleContent());
        }
        Date date = new Date();
        jzJinzhuanModuleDetail.setUpdateDate(date);
        jzJinzhuanModuleDetailService.updateById(jzJinzhuanModuleDetail);
        return Result.ok();
    }



    @ApiOperation(value = "分页多条件查询模块细则信息", notes = "分页多条件查询模块细则信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "JzJinzhuanModuleContent", value = "JzJinzhuanModuleContent", required = true, dataType = "JzJinzhuanModuleContent"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/getModuleContentList")
    public  Result<?> getModuleContentList(@RequestBody JzJinzhuanModuleDetail  jzJinzhuanModuleDetail){
            if (!StringUtils.isBlank(this.getUser().getTrack()) && this.getUser().getTrack().equals("E")){//如果不是那么就是超级管理员，工作人员看不到细则模块
                //竞赛一级管理员
                jzJinzhuanModuleDetail.setCreaterId(this.getUserId());
            }
            return Result.ok(jzJinzhuanModuleContentService.getModuleContentList(jzJinzhuanModuleDetail));
    }



    @ApiOperation(value = "批量删除模块细则信息", notes = "批量删除模块细则信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "模块细则id集合", value = "ids", required = true, dataType = "List<Long>"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @RequestMapping(value = "/deleteModuleContentById", method = RequestMethod.POST)
    public  boolean deleteModuleContentById(@RequestParam("ids[]") Long[] ids){
        return jzJinzhuanModuleContentService.deleteModuleContentById(Arrays.asList(ids));
    }

    @ApiOperation(value = "模板启用禁用按钮", notes = "模板启用禁用按钮")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jzJinzhuanModuleDetail", value = "jzJinzhuanModuleDetail", required = true, dataType = "jzJinzhuanModuleDetail"),
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public boolean updateStatus(JzJinzhuanModuleDetail  jzJinzhuanModuleDetail){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("id",jzJinzhuanModuleDetail.getId());
        boolean b = jzJinzhuanModuleDetailService.update(jzJinzhuanModuleDetail, wrapper);
        return b;
    }

}
